﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Entities;
using BLL;

namespace QLTB_CTK34
{
    public partial class FrmThemPhanQuyenSuDung : Form
    {
        public FrmThemPhanQuyenSuDung ()
        {
             InitializeComponent();
             this.MaximizeBox = false;
        }
        public FrmThemPhanQuyenSuDung(string mataikhoan,string tentk,string chucvu,LoaiQuyen lq):this()
        {
            txtMaTK.Text = mataikhoan;
            txtTenTK.Text = tentk;
            txtChucvu.Text = chucvu;
            LQ = lq;
            
        }

        LoaiQuyen LQ;
        /// <summary>
        
        /// </summary>
        public List<PhanQuyenSuDung> dspqsd;
        private void btnLuu_Click(object sender, EventArgs e)
        {
            dspqsd = new List<PhanQuyenSuDung>();
            foreach (var item in clbPhanQuyenSuDung.CheckedItems)
            {
                QuyenSuDung qsd = Generic.FindByCondition<QuyenSuDung>(x => x.MoTa.Trim() == item.ToString()).ToList()[0];
                PhanQuyenSuDung pqsd = new PhanQuyenSuDung();
                pqsd.MaQuyenSuDung = qsd.MaQuyenSuDung;
                dspqsd.Add(pqsd);
            }
			QLNhatKy.ThemNhatKy("Thêm phân quyền sử dụng");
            DialogResult = DialogResult.OK;

        }
        void LoadPhanQuyenSD()
        {
            dspqsd = QLPhanQuyenSuDung.ThemPhanQuyen(LQ).Result;
            dspqsd.ForEach(x => x.MaTK = txtMaTK.Text);
            var dsqsd = QlQuyenSuDung.GetAll().ToList();
            dsqsd.ForEach(x =>
                {
                    bool check = dspqsd.Any(pqsd => pqsd.MaQuyenSuDung == x.MaQuyenSuDung);
                    clbPhanQuyenSuDung.Items.Add(x.MoTa,check);
                });
        }
        private void FrmThemPhanQuyenSuDung_Load(object sender, EventArgs e)
        {
            LoadPhanQuyenSD();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void cbCheckAll_CheckedChanged(object sender, EventArgs e)
        {
                for (int i = 0; i < clbPhanQuyenSuDung.Items.Count; i++)
                {
                    clbPhanQuyenSuDung.SetItemChecked(i, cbCheckAll.Checked);
                }
            
        }
    }
}
